package algorithm;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

//两数之和：给定一个升序排列的整数数组numbers，从数组中找出两个数满足相加之和等于目标数tar get。
//假设每个输入只对应唯一的答案，而且不可以重复使用相同的元素
//返回两数的下标值，以数组形式发返回
public class Test1 {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(twoPoint(new int[]{1, 2, 3, 4, 5, 6}, 10)));
    }

    //二分查找
    public static int[] twoSearch(int[] nums, int target){
        for(int i=0;i<nums.length;i++){
            int low = i, high = nums.length - 1;
            while (low <= high){
                int mid = (high-low)/2 + low;
                if(nums[mid] == target - nums[i]){
                    return new int[]{i,mid};
                }else if(nums[mid] > target - nums[i]){
                    high = mid - 1;
                }else {
                    low = mid + 1;
                }
            }
        }
        return new int[0];
    }

    //双指针查找
    public static int[] twoPoint(int[] nums,int target){
        int low = 0,high = nums.length - 1;
        while (low < high){
            int sum = nums[low] + nums[high];
            if(sum == target){
                return new int[]{low,high};
            }else if(sum < target){
                 low++;
            }else{
                high--;
            }
        }
        return new int[0];
    }

}
